
package com.cip.master.action;

import java.util.List;
import java.util.Locale;

import com.cip.admin.model.User;
import com.cip.common.util.SecurityUtil;
import com.cip.master.model.ClassYear;
import com.cip.master.model.Section;
import com.cip.master.service.ClassYearService;
import com.cip.master.service.SectionService;
import com.cip.webapp.action.BaseAction;

public class SectionAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private Section section;	
	private SectionService sectionService;
	private List<Section> sectionList;
	private ClassYearService classYearService;
	private List<ClassYear> classYears;
	private String classYear;

	public String showSectionList() throws Exception {
		
		Locale locale = getRequest().getLocale();
		String sectionString = sectionService.getSectionGridList(locale);
		getRequest().setAttribute("sectionGridString", sectionString);
		
//		setSectionList(sectionService.getAllSection());
		return SUCCESS;
	}
	
	public String saveOrUpdateSection() throws Exception{
		User user = SecurityUtil.getThreadLocalUser().get();
		ClassYear classYr=classYearService.getClassYear(Integer.parseInt(classYear));
		section.setClassYear(classYr);
		section.setTenantId(user.getTenantId());
		sectionService.saveOrUpdate(section);
		saveMessage(getText("section.added"));
		return SUCCESS;
	}
	
	public String showEditSection() throws Exception {
		setSection(sectionService.getSection(section.getId()));
		setClassYear(getSection().getClassYear().getName());
		setClassYears(classYearService.getAllClassYear());
		return SUCCESS;
	}
	
	public String showCreateSection() throws Exception {
		setSection(new Section());
		setClassYears(classYearService.getAllClassYear());
		return SUCCESS;
	}

	public void setSectionList(List<Section> sectionList) {
		this.sectionList = sectionList;
	}

	public List<Section> getSectionList() {
		return sectionList;
	}

	public void setSectionService(SectionService sectionService) {
		this.sectionService = sectionService;
	}

	public SectionService getSectionService() {
		return sectionService;
	}

	public void setSection(Section section) {
		this.section = section;
	}

	public Section getSection() {
		return section;
	}

	public void setClassYearService(ClassYearService classYearService) {
		this.classYearService = classYearService;
	}

	public ClassYearService getClassYearService() {
		return classYearService;
	}

	public List<ClassYear> getClassYears() {
		return classYears;
	}

	public void setClassYears(List<ClassYear> classYears) {
		this.classYears = classYears;
	}

	public void setClassYear(String classYear) {
		this.classYear = classYear;
	}

	public String getClassYear() {
		return classYear;
	}	
}
